from constants import FAMILY,GUA_NAMES,LIU_QIN_NUM_60,REMAINDER_OTHER,REMAINDER_SELF, REMINDER_TO_WX


class GuaModel:
    '''包含了一个卦常见的属性'''
    def __init__(self, gua_idx: int):
        self.gua_idx = gua_idx
        
    def get_gua_type(self):
        return _gua_type(self.gua_idx)
    
    def get_self_other_pair(self):
        return search_self(self.gua_idx)

    def get_self_other_list(self,skip=True):
        self_other_lst = [''] * 6
        self_posit,other_posit = self.get_self_other_pair()
        self_other_lst[self_posit] = '世'
        self_other_lst[other_posit] = '应'
        if not skip:
            max_num = max(self_posit, other_posit)
            for i in range(6):
                if max_num - 3 < i < max_num:
                    self_other_lst[i] = '间'
                if i < max_num - 3 or max_num < i:
                    self_other_lst[i] = '边'
        return self_other_lst

    def get_gua_gong(self):
        return _gua_gong(self.gua_idx)
    
    def get_full_gua_name(self):
        return GUA_NAMES[self.gua_idx] + _gua_suffix(self.gua_idx)

    def get_palace_wx(self):
        return REMINDER_TO_WX[self.gua_idx // 8]

    def get_palace12(self):
        return [FAMILY[num] for num in LIU_QIN_NUM_60[self.get_palace_wx()]]


'''以下都是这个类的辅助函数'''
def search_self(gua_idx: int):
    '''
    给卦的序号，找出世应的idx
    :param gua_idx:卦的序号
    :return: List[self_idx,other_idx]（5-0代表初爻到六爻）
    '''
    remain = gua_idx % 8
    return [REMAINDER_SELF[remain], REMAINDER_OTHER[remain]]


def _gua_type(gua_idx):
    if gua_idx in [3, 9, 17, 33, 41, 43, 49, 57]:
        return '六合'
    elif gua_idx % 8 == 0 or gua_idx in [44, 28]:
        return '六冲'
    elif gua_idx % 8 == 6:
        return '游魂'
    elif gua_idx % 8 == 7:
        return '归魂'
    else:
        return ''
def _gua_gong(gua_idx):
    # special for gua_idx ,与constants中的列表不同
    baGuaLst = ['乾', '震', '坎', '巽', '艮', '坤', '离', '兑']
    return baGuaLst[gua_idx // 8]

def _gua_suffix(gua_idx):
    if s := _gua_type(gua_idx):
        s = ',' + s
    return '(' + _gua_gong(gua_idx) + '宫' + s + ')'
